using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

namespace Pampa.WebControls
{
    [ToolboxData("<{0}:MonthCalendar runat=server></{0}:MonthCalendar>")]
    public class MonthCalendar : BaseControl
    {
        string onClientMonthChange;
        int year;
        int month;

        string[] monthNames;

        bool focuseable;

        #region Constructores

        public MonthCalendar()
        {
            this.CssClass = "monthCalendarDefault";
            this.clientClass = "MonthCalendar";

            this.Year = DateTime.Now.Year;
            this.Month = DateTime.Now.Month;
            this.Focuseable = true;

            monthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
        }

        #endregion

        #region Properties

        public int Month
        {
            get { return month; }
            set { month = value; }
        }

        public int Year
        {
            get { return year; }
            set { year = value; }
        }

        public DateTime Date
        {
            get 
            { 
                return new DateTime(year, month, 1); 
            }
            set 
            {
                this.Year = value.Year;
                this.Month = value.Month;
            }
        }

        public string OnClientMonthChange
        {
            get { return onClientMonthChange; }
            set { onClientMonthChange = value; }
        }

        internal bool Focuseable
        {
            get { return focuseable; }
            set { focuseable = value; }
        }

        private string FocusFunction
        {
            get 
            {
                if (this.Focuseable)
                    return "FocusManager.setFocus(\'" + this.ID + "\');";
                else
                    return "";            
            }
        }

        private string ObjectName
        {
            get { return "clr" + this.ID.Replace(".", "_"); }
        }

        #endregion

        #region Load

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (this.CssClass == "monthCalendarDefault")
                RegisterStyleSheet("Pampa.WebControls/MonthCalendar.css");

            RegisterLibrary("Pampa.Common/Common.js");
            RegisterLibrary("Pampa.WebControls/FocusManager.js");
            RegisterLibrary("Pampa.WebControls/MonthCalendar.js");
        }

        #endregion

        #region Render

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "divcont" + this.ID);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            RenderContents(writer);

            writer.RenderEndTag();

            RenderDefinitions(writer);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
            writer.AddAttribute("calendar", "yes");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            // Titulo
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            RenderTitle(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();

            // Meses
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            writer.AddAttribute(HtmlTextWriterAttribute.Id, "mainTable_" + this.ID);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            RenderMonths(writer);
            writer.RenderEndTag();

            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag();
        }

        private void RenderDefinitions(HtmlTextWriter output)
        {
            string objectName = this.ObjectName;

            output.WriteLine("<script type=\"text/javascript\">");
            output.WriteLine("var " + objectName + " = new " + this.clientClass + "(\"" + this.ID + "\");");

            // Properties                
            RenderProperties(output);

            output.WriteLine("</script>");
        }

        protected virtual void RenderProperties(HtmlTextWriter output)
        {
            string objectName = this.ObjectName;

            // Properties
            for (int i = 0; i < 12; i++)
                output.WriteLine(objectName + ".monthNames[" + i.ToString() + "] = \"" + monthNames[i].Substring(0,3).ToUpper() + "\";");

            output.WriteLine(objectName + ".cssClass = \"" + this.CssClass + "\";");
            output.WriteLine(objectName + ".year = " + this.Year + ";");
            output.WriteLine(objectName + ".month = \"" + this.Month + "\";");
            output.WriteLine(objectName + ".onMonthChange = \"" + this.OnClientMonthChange + "\";");
            output.WriteLine(objectName + ".focuseable = " + this.Focuseable.ToString().ToLower() + ";");
        }

        protected void RenderTitle(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("calendar", "yes");
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "javascript:" + this.FocusFunction + "MonthCalendar_PreviousYear(\"" + this.ID + "\")");
            writer.RenderBeginTag(HtmlTextWriterTag.A); 
            writer.Write("<");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Id, "title_" + this.ID);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "title");
            writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(this.Year);
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Align, "right");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("calendar", "yes");
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "javascript:" + this.FocusFunction + "MonthCalendar_NextYear(\"" + this.ID + "\")");
            writer.RenderBeginTag(HtmlTextWriterTag.A); 
            writer.Write(">");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        protected void RenderMonths(HtmlTextWriter writer)
        {
            int month = 1;

            for (int i = 0; i < 4; i++)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                for (int j = 0; j < 3; j++)
                {
                    if (this.Month == month)
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, "selectedMonth");
                    else
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, "month");

                    writer.AddAttribute(HtmlTextWriterAttribute.Id, "month_" + month + "_" + this.ID);
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);

                    writer.AddAttribute("calendar", "yes");
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "javascript:" + this.FocusFunction + "MonthCalendar_MonthChange(\"" + this.ID + "\", " + month + ")");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.Write(monthNames[month-1].Substring(0, 3).ToUpper());
                    writer.RenderEndTag();

                    writer.RenderEndTag();
                    month++;
                }

                writer.RenderEndTag();
            }
        }

        #endregion
    }
}
